package fun.flyee.android.pengshan.acts;

import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.media.Ringtone;
import android.os.Build;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.hcvsa.MeetingForceQuit;
import com.hcvsa.MyHCVSA;
import com.hcvsa.UserAcceptCall;
import com.hcvsa.UserHandUpCall;
import com.hcvsa.UserRejectCall;
import com.squareup.otto.Subscribe;
import com.until.BusProvider;
import com.view.MyVideoRel2;

import butterknife.BindView;
import butterknife.OnClick;
import fun.flyee.android.pengshan.utils.Systems;
import fun.flyee.pengshan.social.R;
import hcvs.hcvsa.HCVSA;
import hcvs.hcvsa.MeetingEvent;

public class ConferenceActivity extends BaseNotifyActivity {

    private int meetingId;
    @BindView(R.id.myVideoRel)
    public MyVideoRel2 myVideoRel;
    @BindView(R.id.root_view)
    public View rootView;
    @BindView(R.id.switch_camera_btn)
    public View cameraBtn;
    @BindView(R.id.switch_mic_btn)
    public ImageView micBtn;
    @BindView(R.id.hangup_btn)
    public View hangupBtn;

    protected int layoutId() {
        return R.layout.act_conference;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD //解锁
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON //保持屏幕不息屏
                | WindowManager.LayoutParams.FLAG_TURN_SCREEN_ON);//点亮屏幕
        Systems.hideSystemNavBar(this);
        if (Build.VERSION.SDK_INT > 27) {
            setShowWhenLocked(true);
        } else {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED);
        }
        WindowManager.LayoutParams params = getWindow().getAttributes();
        params.systemUiVisibility = View.SYSTEM_UI_FLAG_LOW_PROFILE;
        getWindow().setAttributes(params);
        BusProvider.getInstance().register(this);
        meetingId = getIntent().getIntExtra("meetingid", 0);
        initSpeak();
//        bindRootView(rootView);
    }

    @Override
    protected void onResume() {
        super.onResume();
        hideNotify();
    }

    @Override
    public void onDestroy() {
        releaseRing();
        BusProvider.getInstance().unregister(this);
        exit();
        super.onDestroy();
    }

    private void initSpeak() {
        MyHCVSA.getInstance().getMeetingMoudle().JoinMeetingRoom(meetingId, "");
        MyHCVSA.getInstance().OpenMyVideoChannel2(0);
        HCVSA.SetAudioCaptureMute(HCVSA.HCVS_VIDEO_MUTE);
    }

    @OnClick(R.id.switch_camera_btn)
    public void onCameraSwitch() {
        myVideoRel.getCameraSurface(0).cameraFacingChanged();
    }

    @OnClick(R.id.switch_mic_btn)
    public void onMicSwitch() {
        HCVSA.SetAudioCaptureMute(HCVSA.GetAudioCaptureMute() == HCVSA.HCVS_VIDEO_MUTE ? HCVSA.HCVS_VIDEO_NOT_MUTE : HCVSA.HCVS_VIDEO_MUTE);
        micBtn.setImageResource(HCVSA.GetAudioCaptureMute() == HCVSA.HCVS_VIDEO_MUTE ? R.mipmap.mic_off : R.mipmap.mic_on);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            this.hangup();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    @Subscribe
    public void UserHandUpCall(UserHandUpCall data) {
        Toast.makeText(this, "会议结束", Toast.LENGTH_LONG).show();
        finish();
    }

    @Subscribe
    public void UserForceQuit(MeetingForceQuit data) {
        Toast.makeText(this, "被请出会议", Toast.LENGTH_LONG).show();
        finish();
    }

    @OnClick(R.id.hangup_btn)
    public void hangup() {
        finish();
    }

    @Override
    public void onPause() {
        super.onPause();
        showNotify();
    }

    private void exit() {
        //关闭我的摄像头
        MyHCVSA.getInstance().CloseMyVideoChannel2(0);
        //退出会议室
        MyHCVSA.getInstance().getMeetingMoudle().ExitMeetingRoom();
        //释放资源
        if (myVideoRel != null) {
            try {
                myVideoRel.release();
            } catch (Throwable throwable) {
                throwable.printStackTrace();
            }
        }
    }

//    @Override
//    public void finish() {
//        startActivity(new Intent(this, BlankActivity.class));
//        super.finish();
//    }

//    @OnClick(R.id.float_btn)
//    public void onFloatClick() {
//        super.toFloat();
//        cameraBtn.setVisibility(View.GONE);
//        hangupBtn.setVisibility(View.GONE);
//    }
//    @OnClick(R.id.fixed_btn)
//    public void onFixedClick() {
//        super.toFixed();
//        cameraBtn.setVisibility(View.VISIBLE);
//        hangupBtn.setVisibility(View.VISIBLE);
//    }
}
